import dayjs from "dayjs";
let vm = process.env.NODE_ENV === 'production' ? require('@/main-prod.js') : require('@/main-dev.js')
import * as timezone from "dayjs/plugin/timezone"; // import plugin
import * as utc from "dayjs/plugin/utc"; // import plugin
import {changeNum} from "@/assets/js/filters"; // import plugin
// import * as _ from 'lodash';
// 配置使用处理相对时间的插件
dayjs.extend(utc);
dayjs.extend(timezone);
let bgColor = "";//背景
let upColor//涨颜色
let downColor//跌颜色
let timeColor = []; // 分时颜色列表
let KColorList = []; // K线颜色列表
let securityType = [];
export function judgeColor() {
  if (sessionStorage.getItem("color")=='RedRose') { 
     // eslint-disable-next-line no-mixed-spaces-and-tabs
    upColor="#DD3F3F";//涨颜色
    downColor="#15A76B";//跌颜色
  } else {
    upColor="#15A76B";//跌颜色
    downColor="#DD3F3F";//涨颜色
  }
};
let market = ""; // 市场
let yClose = ""; // 昨收
let coded = ""; // K线类型类型
let lang = localStorage.getItem("lang");
export function getTimezone(val) {
  market = val.market;
  yClose = val.close;
  coded = val.coded;
  securityType = val.securityType;
};
export const config = {
    // 网格线
    grid: {
      show: true,
      // 网格水平线
      horizontal: {
        show: true,
        size: 1,
        color: '#212538',
        // 'solid'|'dash'
        style: 'solid',
        dashValue: [2, 2]
      },
      // 网格垂直线 
      vertical: {
        show: true,
        size: 1,
        color: '#212538',
        // 'solid'|'dash'
        style: 'solid',
        dashValue: [2, 2]
      }
    },
    // 蜡烛图  
    candle: {
      // 蜡烛图上下间距，大于1为绝对值，大于0小余1则为比例
      margin: {
        top: 0.2,
        bottom: 0.1
      },
      // 蜡烛图类型 'candle_solid'|'candle_stroke'|'candle_up_stroke'|'candle_down_stroke'|'ohlc'|'area'
      type: 'candle_up_stroke',
      // 蜡烛柱
      bar: {
        upColor: "#DD3F3F",
        downColor: "#15A76B",
        noChangeColor: '#888888'
      },
      // 面积图
      area: {
        lineSize: 2,
        lineColor: '#448ef6',
        value: 'close',
        fillColor: [{
          offset: 0,
          color: 'rgba(33, 150, 243, 0.01)'
        }, {
          offset: 1,
          color: 'rgba(33, 150, 243, 0.2)'
        }]
      },
      priceMark: {
        show: true,
        // 最高价标记  
        high: {
          show: true,
          color: "#DD3F3F",
          textMargin: 5,
          textSize: 10,
          textFamily: 'Helvetica Neue',
          textWeight: 'normal'
        },
        // 最低价标记  
        low: {
          show: true,
          color: "#15A76B",
          textMargin: 5,
          textSize: 10,
          textFamily: 'Helvetica Neue',
          textWeight: 'normal',
        },
        // 最新价标记    
        last: {
          show: true,
          upColor: '#26A69A',
          downColor: '#EF5350',
          noChangeColor: '#888888',
          line: {
            show: true,
            // 'solid'|'dash'
            style: 'dash',
            dashValue: [4, 4],
            size: 1
          },
          text: {
            show: true,
            size: 12,
            paddingLeft: 2,
            paddingTop: 2,
            paddingRight: 2,
            paddingBottom: 2,
            color: '#FFFFFF',
            family: 'Helvetica Neue',
            weight: 'normal'
          }
        }
      },
      // 提示  
      tooltip: {
        showRule: 'always',
        showType: 'rect',
        labels: ['时间', '开盘价', '收盘价', '最高价', '最低价', '成交量'],
        values: (params)=> {
          let values = [];
          let isDWM = false; // 是否是日月周K线
          // 日K、周K、月K：开盘、最高、最低、收盘、涨跌额、涨跌幅、成交量、成交额、换手率，（开盘、最高、最低、收盘、涨跌额、涨跌幅）带有颜色，与昨收进行对比
          if(coded == 'day' || coded == 'week' || coded == 'month') {
            isDWM = true;
            if(yClose > params.open) KColorList[0] = downColor;
            else if(yClose < params.open) KColorList[0] = upColor;
            else KColorList[0] = "#989EB3";
            if(yClose > params.close) KColorList[1] = downColor;
            else if(yClose < params.close) KColorList[1] = upColor;
            else KColorList[1] = "#989EB3";
            if(yClose > params.high) KColorList[2] = downColor;
            else if(yClose < params.high) KColorList[2] = upColor;
            else KColorList[2] = "#989EB3";
            if(yClose > params.low) KColorList[3] = downColor;
            else if(yClose < params.low) KColorList[3] = upColor;
            else KColorList[3] = "#989EB3";
            values = [
              {
                value: params.timestamp,
              },
              {
                value: params.open,
                color: KColorList[0]
              },
              {
                value: params.close,
                color: KColorList[1]
              },
              {
                value: params.high,
                color: KColorList[2]
              },
              {
                value: params.low,
                color: KColorList[3]
              },
              {
                value: params.volume,
              },
            ];
          }else {
            values = [
              {
                value: params.timestamp,
              },
              {
                value: params.open,
                color: "#989EB3"
              },
              {
                value: params.close,
                color: "#989EB3"
              },
              {
                value: params.high,
                color: "#989EB3"
              },
              {
                value: params.low,
                color: "#989EB3"
              },
              {
                value: params.volume,
              },
            ];
          }
          values.forEach(function (val, index) {
            switch (index) {
              case 0:
                {
                  if(market == "US") {
                    // 如果是日周月K线显示年月日星期
                    if(isDWM) {
                      if(lang == "US")
                        values[index].value = dayjs(val.value).tz("America/Toronto").format("YYYY/MM/DD dddd");
                      else values[index].value = dayjs(val.value).tz("America/Toronto").format("YYYY/MM/DD 星期d");
                    }else values[index].value = dayjs(val.value).tz("America/Toronto").format("MM/DD HH:mm");
                  }else {
                    // 其他股市显示北京时间
                    if(isDWM) {
                      if(lang == "US")
                        values[index].value = dayjs(val.value).format("YYYY/MM/DD dddd");
                      else values[index].value = dayjs(val.value).format("YYYY/MM/DD 星期d");
                    }else values[index].value = dayjs(val.value).format("MM/DD HH:mm");
                  }
                  break;
                }
              case values.length - 1:
                {
                  values[index].value = changeNum(val.value,vm.te);
                  break;
                }
            }
          });
          return values;
        },
        // values: null,
        defaultValue: 'n/a',
        rect: {
          paddingLeft: 0,
          paddingRight: 0,
          paddingTop: 0,
          paddingBottom: 6,
          offsetLeft: 20,
          offsetTop: 60,
          offsetRight: 8,
          borderRadius: 4,
          borderSize: 1,
          borderColor: '#3f4254',
          fillColor: 'rgba(17, 17, 17, .9)'
        },
        text: {
          size: 12,
          family: 'Helvetica Neue',
          weight: 'normal',
          color: '#989EB3',
          marginLeft: 8,
          marginTop: 6,
          marginRight: 8,
          marginBottom: 0
        }
      }
    },
    // 技术指标  
    technicalIndicator: {
      margin: {
        top: 0.2,
        bottom: 0.1
      },
      bar: {
        upColor: "#DD3F3F",
        downColor: "#15A76B",
        noChangeColor: '#888888'
      },
      line: {
        size: 1,
        colors: ['#f69802', '#9D65C9', '#2196F3', '#E11D74', '#01C5C4']
      },
      circle: {
        upColor: "#DD3F3F",
        downColor: "#15A76B",
        noChangeColor: '#888888'
      },
      // 最新值标记  
      lastValueMark: {
        show: false,
        text: {
          show: false,
          color: '#ffffff',
          size: 12,
          family: 'Helvetica Neue',
          weight: 'normal',
          paddingLeft: 3,
          paddingTop: 2,
          paddingRight: 3,
          paddingBottom: 2
        }
      },
      // 提示  
      tooltip: {
        showRule: 'follow_cross',
        showName: true,
        showParams: true,
        defaultValue: 'n/a',
        text: {
          size: 12,
          family: 'Helvetica Neue',
          weight: 'normal',
          color: '#D9D9D9',
          marginTop: 6,
          marginRight: 8,
          marginBottom: 0,
          marginLeft: 8
        }
      }
    },
    // x轴
    xAxis: {
      show: true,
      height: null,
      // x轴线
      axisLine: {
        show: true,
        color: '#212538',
        size: 1
      },
      // x轴分割文字
      tickText: {
        show: true,
        color: '#D9D9D9',
        family: 'Helvetica Neue',
        weight: 'normal',
        size: 12,
        paddingTop: 3,
        paddingBottom: 6
      },
      // x轴分割线  
      tickLine: {
        show: true,
        size: 1,
        length: 3,
        color: '#888888'
      }
    },
    // y轴  
    yAxis: {
      show: true,
      width: null,
      // 'left' | 'right'
      position: 'right',
      // 'normal' | 'percentage'
      type: 'normal',
      inside: false,
      // y轴线  
      axisLine: {
        show: true,
        color: '#212538',
        size: 1
      },
      // x轴分割文字  
      tickText: {
        show: true,
        color: '#D9D9D9',
        family: 'Helvetica Neue',
        weight: 'normal',
        size: 12,
        paddingLeft: 3,
        paddingRight: 6
      },
      // x轴分割线
      tickLine: {
        show: true,
        size: 1,
        length: 3,
        color: '#888888'
      }
    },
    // 图表之间的分割线  
    separator: {
      size: 1,
      color: '#888888',
      fill: true,
      activeBackgroundColor: 'rgba(230, 230, 230, .15)'
    },
    // 十字光标  
    crosshair: {
      show: true,
      // 十字光标水平线及文字
      horizontal: {
        show: true,
        line: {
          show: true,
          // 'solid'|'dash'
          style: 'dash',
          dashValue: [4, 2],
          size: 1,
          color: '#4d5462'
        },
        text: {
          show: true,
          color: '#D9D9D9',
          size: 12,
          family: 'Helvetica Neue',
          weight: 'normal',
          paddingLeft: 2,
          paddingRight: 2,
          paddingTop: 2,
          paddingBottom: 2,
          borderSize: 1,
          borderColor: '#505050',
          backgroundColor: '#505050'
        }
      },
      // 十字光标垂直线及文字  
      vertical: {
        show: true,
        line: {
          show: true,
          // 'solid'|'dash'
          style: 'dash',
          dashValue: [4, 2],
          size: 1,
          color: '#4d5462'
        },
        text: {
          show: true,
          color: '#D9D9D9',
          size: 12,
          family: 'Helvetica Neue',
          weight: 'normal',
          paddingLeft: 2,
          paddingRight: 2,
          paddingTop: 2,
          paddingBottom: 2,
          borderSize: 1,
          borderColor: '#505050',
          backgroundColor: '#505050'
        }
      }
    },
    // 图形标记  
    graphicMark: {
      line: {
        color: '#2196F3',
        size: 1
      },
      point: {
        backgroundColor: '#2196F3',
        borderColor: '#2196F3',
        borderSize: 1,
        radius: 4,
        activeBackgroundColor: '#2196F3',
        activeBorderColor: '#2196F3',
        activeBorderSize: 1,
        activeRadius: 6
      },
      polygon: {
        stroke: {
          size: 1,
          color: '#2196F3'
        },
        fill: {
          color: 'rgba(33, 150, 243, 0.1)'
        }
      },
      arc: {
        stroke: {
          size: 1,
          color: '#2196F3'
        },
        fill: {
          color: 'rgba(33, 150, 243, 0.1)'
        }
      },
      text: {
        color: '#2196F3',
        size: 12,
        family: 'Helvetica Neue',
        weight: 'normal',
        marginLeft: 2,
        marginRight: 2,
        marginTop: 2,
        marginBottom: 6
      }
    }
};
export const configs = {
    // 网格线
    grid: {
      show: true,
      // 网格水平线
      horizontal: {
        show: true,
        size: 1,
        color: '#212538',
        // 'solid'|'dash'
        style: 'solid',
        dashValue: [2, 2]
      },
      // 网格垂直线 
      vertical: {
        show: true,
        size: 1,
        color: '#212538',
        // 'solid'|'dash'
        style: 'solid',
        dashValue: [2, 2]
      }
    },
    // 蜡烛图  
    candle: {
      // 蜡烛图上下间距，大于1为绝对值，大于0小余1则为比例
      margin: {
        top: 0.2,
        bottom: 0.1
      },
      // 蜡烛图类型 'candle_solid'|'candle_stroke'|'candle_up_stroke'|'candle_down_stroke'|'ohlc'|'area'
      type: 'area',
      // 蜡烛柱
      bar: {
        upColor: "#DD3F3F",//涨颜色
        downColor: "#15A76B",//跌颜色
        noChangeColor: '#888888'
      },
      // 面积图
      area: {
        lineSize: 2,
        lineColor: '#448ef6',
        value: 'close',
        fillColor: [{
          offset: 0,
          color: 'rgba(33, 150, 243, 0.01)'
        }, {
          offset: 1,
          color: 'rgba(33, 150, 243, 0.2)'
        }]
      },
      priceMark: {
        show: true,
        // 最高价标记  
        high: {
          show: true,
          color: "#DD3F3F",
          textMargin: 5,
          textSize: 10,
          textFamily: 'Helvetica Neue',
          textWeight: 'normal'
        },
        // 最低价标记  
        low: {
          show: true,
          color: "#15A76B",
          textMargin: 5,
          textSize: 10,
          textFamily: 'Helvetica Neue',
          textWeight: 'normal',
        },
        // 最新价标记    
        last: {
          show: true,
          upColor: '#26A69A',
          downColor: '#EF5350',
          noChangeColor: '#888888',
          line: {
            show: true,
            // 'solid'|'dash'
            style: 'dash',
            dashValue: [4, 4],
            size: 1
          },
          text: {
            show: true,
            size: 12,
            paddingLeft: 2,
            paddingTop: 2,
            paddingRight: 2,
            paddingBottom: 2,
            color: '#FFFFFF',
            family: 'Helvetica Neue',
            weight: 'normal'
          }
        }
      },
      // 提示  
      tooltip: {
        showRule: 'always',
        showType: 'rect',
        labels: ['时间', '价格', '均价','涨跌额', '涨跌幅', '成交量', '成交额'],
        values: (params)=> {
          let values = [];
          // 分时价格、涨跌额、涨跌幅颜色变化
          if(coded == 'day' || coded == '5day') {
            if(yClose > params.close) timeColor[0] = downColor;
            else if(yClose < params.close) timeColor[0] = upColor;
            else timeColor[0] = "#989EB3";
            if(securityType == 1)
              values = [
              {
                value: params.timestamp,
              },
              {
                value: params.close,
                color: timeColor[0]
              },
              {
                value: (params.turnover / params.volume).toFixed(2),
              },
              {
                value: changeNum(params.chg, vm.te),
                color: timeColor[0]
              },
              {
                value: params.gain + '%',
                color: timeColor[0]
              },
              {
                value: changeNum(params.volume, vm.te),
              },
              {
                value: changeNum(params.turnover, vm.te),
              },
              ];
            else
            values = [
              {
                value: params.timestamp,
              },
              {
                value: params.close,
                color: timeColor[0]
              },
              {
                value: changeNum(params.chg, vm.te),
                color: timeColor[0]
              },
              {
                value: params.gain + '%',
                color: timeColor[0]
              },
              {
                value: changeNum(params.volume, vm.te),
              },
              ];
          }else {
            values = [
              {
                value: params.timestamp,
              },
              {
                value: params.close,
              },
              {
                value: params.volume,
              },
              {
                value: params.turnover,
              },
            ];
          };
          values.forEach(function (val, index) {
            switch (index) {
              case 0:
                {
                  if(market == "US") {
                    values[index].value = dayjs(val.value).tz("America/Toronto").format("MM/DD HH:mm");
                  }else values[index] = dayjs(val.value).format("MM/DD HH:mm");
                  break;
                }
            }
          });
          return values;
        },
        defaultValue: 'n/a',
        rect: {
          paddingLeft: 0,
          paddingRight: 0,
          paddingTop: 0,
          paddingBottom: 6,
          offsetLeft: 20,
          offsetTop: 60,
          offsetRight: 8,
          borderRadius: 4,
          borderSize: 1,
          borderColor: '#3f4254',
          fillColor: 'rgba(17, 17, 17, .9)'
        },
        text: {
          size: 12,
          family: 'Helvetica Neue',
          weight: 'normal',
          color: '#D9D9D9',
          marginLeft: 8,
          marginTop: 6,
          marginRight: 8,
          marginBottom: 0
        }
      }
    },
    // 技术指标  
    technicalIndicator: {
      margin: {
        top: 0.2,
        bottom: 0.1
      },
      bar: {
        upColor: "#DD3F3F",//涨颜色
        downColor: "#15A76B",//跌颜色
        noChangeColor: '#888888'
      },
      line: {
        size: 1,
        colors: ['#f69802', '#9D65C9', '#2196F3', '#E11D74', '#01C5C4']
      },
      circle: {
        upColor: "#DD3F3F",//涨颜色
        downColor: "#15A76B",//跌颜色
        noChangeColor: '#888888'
      },
      // 最新值标记  
      lastValueMark: {
        show: false,
        text: {
          show: false,
          color: '#ffffff',
          size: 12,
          family: 'Helvetica Neue',
          weight: 'normal',
          paddingLeft: 3,
          paddingTop: 2,
          paddingRight: 3,
          paddingBottom: 2
        }
      },
      // 提示  
      tooltip: {
        showRule: 'none',
        showName: true,
        showParams: true,
        defaultValue: 'n/a',
        text: {
          size: 12,
          family: 'Helvetica Neue',
          weight: 'normal',
          // color: '#D9D9D9',
          color: '#989EB3',
          marginTop: 6,
          marginRight: 8,
          marginBottom: 0,
          marginLeft: 8
        }
      }
    },
    // x轴
    xAxis: {
      show: true,
      height: null,
      // x轴线
      axisLine: {
        show: true,
        color: '#212538',
        size: 1
      },
      // x轴分割文字
      tickText: {
        show: true,
        color: '#D9D9D9',
        family: 'Helvetica Neue',
        weight: 'normal',
        size: 12,
        paddingTop: 3,
        paddingBottom: 6
      },
      // x轴分割线  
      tickLine: {
        show: true,
        size: 1,
        length: 3,
        color: '#888888'
      }
    },
    // y轴  
    yAxis: {
      show: true,
      width: null,
      // 'left' | 'right'
      position: 'right',
      // 'normal' | 'percentage'
      type: 'normal',
      inside: false,
      // y轴线  
      axisLine: {
        show: true,
        color: '#212538',
        size: 1
      },
      // x轴分割文字  
      tickText: {
        show: true,
        color: '#D9D9D9',
        family: 'Helvetica Neue',
        weight: 'normal',
        size: 12,
        paddingLeft: 3,
        paddingRight: 6
      },
      // x轴分割线
      tickLine: {
        show: true,
        size: 1,
        length: 3,
        color: '#888888'
      }
    },
    // 图表之间的分割线  
    separator: {
      size: 1,
      color: '#888888',
      fill: true,
      activeBackgroundColor: 'rgba(230, 230, 230, .15)'
    },
    // 十字光标  
    crosshair: {
      show: true,
      // 十字光标水平线及文字
      horizontal: {
        show: true,
        line: {
          show: true,
          // 'solid'|'dash'
          style: 'dash',
          dashValue: [4, 2],
          size: 1,
          color: '#888888'
        },
        text: {
          show: true,
          color: '#D9D9D9',
          size: 12,
          family: 'Helvetica Neue',
          weight: 'normal',
          paddingLeft: 2,
          paddingRight: 2,
          paddingTop: 2,
          paddingBottom: 2,
          borderSize: 1,
          borderColor: '#505050',
          backgroundColor: '#505050'
        }
      },
      // 十字光标垂直线及文字  
      vertical: {
        show: true,
        line: {
          show: true,
          // 'solid'|'dash'
          style: 'dash',
          dashValue: [4, 2],
          size: 1,
          color: '#888888'
        },
        text: {
          show: true,
          color: '#D9D9D9',
          size: 12,
          family: 'Helvetica Neue',
          weight: 'normal',
          paddingLeft: 2,
          paddingRight: 2,
          paddingTop: 2,
          paddingBottom: 2,
          borderSize: 1,
          borderColor: '#505050',
          backgroundColor: '#505050'
        }
      }
    },
    // 图形标记  
    graphicMark: {
      line: {
        color: '#2196F3',
        size: 1
      },
      point: {
        backgroundColor: '#2196F3',
        borderColor: '#2196F3',
        borderSize: 1,
        radius: 4,
        activeBackgroundColor: '#2196F3',
        activeBorderColor: '#2196F3',
        activeBorderSize: 1,
        activeRadius: 6
      },
      polygon: {
        stroke: {
          size: 1,
          color: '#2196F3'
        },
        fill: {
          color: 'rgba(33, 150, 243, 0.1)'
        }
      },
      arc: {
        stroke: {
          size: 1,
          color: '#2196F3'
        },
        fill: {
          color: 'rgba(33, 150, 243, 0.1)'
        }
      },
      text: {
        color: '#2196F3',
        size: 12,
        family: 'Helvetica Neue',
        weight: 'normal',
        marginLeft: 2,
        marginRight: 2,
        marginTop: 2,
        marginBottom: 6
      }
    }
};
